<?php defined('InShopNC') or exit('Access Invalid!'); ?>
<style type="text/css">
    .content-warp {
    }

    .content-warp div {
        float: left;
        width: 50%;
    }

    .content-warp .content-table {
    }

    .content-warp .content-map {
        height: 310px;
    }

    .rowform .txt, .rowform textarea {
        margin-right: 10px;
        width: 400px;
    }
</style>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <h3>加盟商管理</h3>
            <ul class="tab-base">
                <li><a href="index.php?act=franchise&op=index"><span><?php echo $lang['nc_manage']; ?></span></a></li>
                <li><a href="JavaScript:void(0);"
                       class="current"><span><?php echo $_GET['op'] == 'add' ? '新增' : '编辑'; ?></span></a></li>
            </ul>
        </div>
    </div>
    <div class="fixed-empty"></div>
    <form id="franchise_form" action="" enctype="multipart/form-data" method="post">
        <input type="hidden" name="form_submit" value="ok"/>
        <input type="hidden" name="franchisee_id" value="<?php echo $output['data'] ? $output['data']['franchisee_id'] : ''; ?>"/>
        <input type="hidden" name="mapPoint" id="mapPoint"
               value="<?php echo $output['data'] ? $output['data']['mapPoint'] : ''; ?>"/>
        <input type="hidden" name="areaid_1" id="areaid_1"
               value="<?php echo $output['data'] ? $output['data']['areaid_1'] : ''; ?>"/>
        <input type="hidden" name="areaid_2" id="areaid_2" value="<?php echo $output['data'] ? $output['data']['areaid_2'] : ''; ?>"/>

        <div class="content-warp">
            <div class="content-table">
                <table class="table tb-type2">
                    <tbody>
                    <tr class="noborder">
                        <td colspan="2" class="required"><label class="validation" for="franchisee_name">加盟商名称:</label>
                        </td>
                    </tr>
                    <tr class="noborder">
                        <td class="vatop rowform"><input type="text"
                                                         value="<?php echo $output['data'] ? $output['data']['franchisee_name'] : ''; ?>"
                                                         name="franchisee_name" id="franchisee_name" class="txt"></td>
                        <td class="vatop tips"></td>
                    </tr>
                    <tr class="noborder">
                        <td colspan="2" class="required"><label class="validation"
                                                                for="franchisee_address">加盟商地址:</label></td>
                    </tr>
                    <tr class="noborder">
                        <td class="vatop rowform">
                            <input type="text"
                                   value=""
                                   name="franchisee_address" id="franchisee_address" class="txt">

                            <div id="searchResultPanel"
                                 style="border:1px solid #C0C0C0;width:250px;height:auto; display:none;"></div>
                        </td>
                        <td class="vatop tips"></td>
                    </tr>
                    <tr class="noborder">
                        <td colspan="2" class="required"><label class="validation" for="linkname">联系人:</label></td>
                    </tr>
                    <tr class="noborder">
                        <td class="vatop rowform"><input type="text"
                                                         value="<?php echo $output['data'] ? $output['data']['linkname'] : ''; ?>"
                                                         name="linkname" id="linkname" class="txt"></td>
                        <td class="vatop tips"></td>
                    </tr>
                    <tr>
                        <td colspan="2" class="required"><label class="validation" for="linkname_mobile">联系人电话:</label>
                        </td>
                    </tr>
                    <tr class="noborder">
                        <td class="vatop rowform"><input type="text"
                                                         value="<?php echo $output['data'] ? $output['data']['linkname_mobile']:''; ?>"
                                                         id="linkname_mobile" name="linkname_mobile" class="txt"></td>
                        <td class="vatop tips"></td>
                    </tr>
                    </tbody>
                    <tfoot>
                    <tr class="tfoot">
                        <td colspan="15"><a href="JavaScript:void(0);" class="btn"
                                            id="submitBtn"><span><?php echo $lang['nc_submit']; ?></span></a></td>
                    </tr>
                    </tfoot>
                </table>
            </div>
            <div class="content-map" id="baidu_map"></div>
        </div>
    </form>
</div>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL; ?>/js/dialog/dialog.js" id="dialog_js"
        charset="utf-8"></script>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL; ?>/js/jquery-ui/jquery.ui.js"></script>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL; ?>/js/ajaxfileupload/ajaxfileupload.js"></script>
<script type="text/javascript" src="<?php echo RESOURCE_SITE_URL; ?>/js/jquery.Jcrop/jquery.Jcrop.js"></script>
<script type="text/javascript" src="<?php echo SHOP_SITE_URL; ?>/data/resource/js/area_array.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=P2FNtzxNufSlwBx3Gt3GYhxR"></script>
<script type="text/javascript"
        src="http://api.map.baidu.com/library/MarkerManager/1.2/src/MarkerManager_min.js"></script>
<link href="<?php echo RESOURCE_SITE_URL; ?>/js/jquery.Jcrop/jquery.Jcrop.min.css" rel="stylesheet" type="text/css"
      id="cssfile2"/>
<script type="text/javascript">
    var addressInputId = 'franchisee_address', addressNode = $("#" + addressInputId),
        mapPointNode = $("#mapPointNode"), searchResultPanelNode = document.getElementById('searchResultPanel'),
        areaid_1=$("#areaid_1"),areaid_2=$("#areaid_2"),defaultAddress=<?php var_export($output['data'] ? $output['data']['franchisee_address'] : ''); ?>;
    // 百度地图API功能
    var map = new BMap.Map("baidu_map");
    var geoc = new BMap.Geocoder();//经纬度转地址对象
    //设置中心点102.729099,24.993223
    var point = new BMap.Point(<?php echo $output['data'] ? $output['data']['mapPoint'] : '102.729099, 24.993223'; ?>);
    map.centerAndZoom(point, 19);
    //添加标注管理器
    var mgr = new BMapLib.MarkerManager(map, {trackMarkers: true}), marker;
    //添加标签
    marker=new BMap.Marker(point);
    //监听标注拖拽功能
    marker.addEventListener('dragend', function (e) {
        setAddressByPoint(e.point);
    });
    mgr.addMarker(marker);
    //单击获取点击的经纬度
    map.addEventListener("click", function (e) {
        setAddressByPoint(e.point);
    });
    mgr.showMarkers();
    /**
     * 设置地址经纬度坐标
     * @param point
     */
    function setAddressByPoint(point) {
        //设置标注的地址
        marker.setPosition(point);
        //设置地图中心
        map.centerAndZoom(point, 18);
        //设置经纬度
        mapPointNode.val(point.lng + ',' + point.lat);
        var isProvince=false,isCity=false;
        geoc.getLocation(point, function (rs) {
            var addComp = rs.addressComponents;
            //设置加盟商所在城市及地区
            var province=addComp.province.replace(/省$/,'');
            var city=addComp.city.replace(/市$/,'');
            $.each(nc_a[0],function(i,itemP){
                if(itemP[1].indexOf(province)==0){
                    areaid_1.val(itemP[0]);
                    $.each(nc_a[itemP[0]],function(ic,itemC){
                        if(itemC[1].indexOf(city)==0){
                            areaid_2.val(itemC[0]);
                            isCity=true;
                            return false;
                        }
                    });
                    isProvince=true;
                    return false;
                }
            });
            if(!isProvince){
                areaid_1.val(nc_a[0][nc_a[0].length-1][0]);
            }
            if(!isCity){
                var aid1=areaid_1.val();
                areaid_2.val(nc_a[aid1][nc_a[aid1].length-1][0]);
            }
            var myValue = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
            if (defaultAddress!=myValue) {
                //设置加盟商地址
                addressNode.val(myValue);
            }
        });
    }

    //建立一个自动完成的对象
    var ac = new BMap.Autocomplete({"input": addressInputId, "location": map});
    ac.setInputValue(defaultAddress);
    //鼠标放在下拉列表上的事件
    ac.addEventListener("onhighlight", function (e) {
        var str = "";
        var _value = e.fromitem.value;
        var value = "";
        if (e.fromitem.index > -1) {
            value = _value.province + _value.city + _value.district + _value.street + _value.business;
        }
        str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;
        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province + _value.city + _value.district + _value.street + _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
        searchResultPanelNode.innerHTML = str;
    });


    ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        var myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
        defaultAddress=myValue;
        searchResultPanelNode.innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
        //通过地址得到经纬度
        var local = new BMap.LocalSearch(map, { //智能搜索
            onSearchComplete: function () {
                var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                setAddressByPoint(pp);
            }
        });
        local.search(myValue);
    });

</script>
<script type="text/javascript">
  $(function(){
    //按钮先执行验证再提交表单
    var franchiseForm=$("#franchise_form")[0];
    $("#submitBtn").click(function(){
      if($(franchiseForm).valid()){
        $(franchiseForm).submit();
      }
    });
    $(franchiseForm).validate({
      errorPlacement: function(error, element){
        error.appendTo(element.parent().parent().prev().find('td:first'));
      },
      rules : {
          franchisee_name:{required : true},
          franchisee_address:{required : true},
          linkname:{required : true},
          linkname_mobile:{required : true}
      },
      messages : {
          franchisee_name:{required : '加盟商名称不能为空'},
          franchisee_address:{required : '加盟商地址不能为空'},
          linkname:{required : '联系人不能为空'},
          linkname_mobile:{required : '联系人电话不能为空'}
      }
    });
  });


</script>
